package com.xmall.auth.utils;

import com.xmall.auth.define.constants.AuthProperties;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 密码加密工具
 */
@Component
public class PasswordUtils {

    @Resource
    private AuthProperties authProperties;

    @Resource
    private PasswordEncoder passwordEncoder;

    private static PasswordUtils passwordUtils;

    @PostConstruct
    public void init(){
        passwordUtils = this;
        passwordUtils.passwordEncoder = passwordEncoder;
        passwordUtils.authProperties = authProperties;
    }

    /**
     * 初始化密码
     */
    public static String initPassword(){
        return passwordUtils.passwordEncoder.encode(passwordUtils.authProperties.getInitPassword());
    }

    /**
     * 校验密码
     */
    public static Boolean checkPassword(String rawPassword, String encodedPassword){
        return passwordUtils.passwordEncoder.matches(rawPassword, encodedPassword);
    }
}
